$(function () {
  //需求1:初始化文章分类列表
  //封装成函数,方便以后调用
  let layer = layui.layer;
  initArtCateList();
  function initArtCateList() {
    axios({
      method: "GET",
      url: "/my/article/cates",
      //params:
    }).then((res) => {
      //console.log(res.data);
      if (res.data.status !== 0) {
        return layer.msg(res.data.message);
      }
      //获取成功后渲染出来
      //定义一个空数组,和序号
      let arr = [];
      let index = 0;
      res.data.data.forEach((ele) => {
        index++;
        arr.push(`
        <tr>
         <td>${index}</td>
         <td>${ele.name}</td>
         <td>${ele.alias}</td>
         <td>
           <button data-id='${ele.Id}' class="btn-edit layui-btn layui-btn layui-btn-xs">编辑</button>
           <button data-id='${ele.Id}'
             class="btn-deleted layui-btn layui-btn-xs layui-btn-xs layui-btn-sm layui-btn-danger"
           >
             删除
           </button>
         </td>
        </tr>
        `);
      });
      //循环完毕,赋值到页面
      $("tbody").empty().html(arr.join(""));
    });
  }

  //需求2:点击添加,弹出对话框
  //定义添加分类的索引值
  let indexAdd = null;
  $("#btnAdd").on("click", function () {
    //index=
    indexAdd = layer.open({
      type: 1,
      title: "添加文章分类",
      area: ["500px", "300px"],
      content: `
      <form class="layui-form" action="" id='formAdd'>
      <div class="layui-form-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
          <input
            type="text"
            name="name"
            required
            lay-verify="required"
            placeholder="请输入分类名称"
            autocomplete="off"
            class="layui-input"
          />
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">分类别名</label>
        <div class="layui-input-block">
          <input
            type="text"
            name="alias"
            required
            lay-verify="required"
            placeholder="请输入分类别名"
            autocomplete="off"
            class="layui-input"
          />
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="formDemo">
            立即提交
          </button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
      `,
    });
  });

  //需求3:添加文章,并调用initArtCateList()渲染出来
  $("body").on("submit", "#formAdd", function (e) {
    e.preventDefault();
    axios({
      method: "POST",
      url: "/my/article/addcates",
      data: $(this).serialize(),
    }).then((res) => {
      //console.log(res.data);

      //TODO:判断,关闭对话框,清空表单,刷新页面
      if (res.data.status !== 0) {
        return layer.msg(res.data.message);
      }
      //成功提示
      layer.msg("添加成功");
      layer.close(indexAdd);
      initArtCateList();
    });
  });

  //需求4:点击编辑,弹出对话框,事件委托
  let indexEdit = null;
  let form = layui.form;
  $("tbody").on("click", ".btn-edit", function () {
    indexEdit = layer.open({
      type: 1,
      title: "修改文章分类",
      area: ["500px", "300px"],
      content: `
        <form lay-filter="formEdit" class="layui-form" action="" id='formEdit'>
        <input type="hidden" name="Id">
        <div class="layui-form-item">
        
          <label class="layui-form-label">分类名称</label>
          <div class="layui-input-block">
            <input
              type="text"
              name="name"
              required
              lay-verify="required"
              placeholder="请输入分类名称"
              autocomplete="off"
              class="layui-input"
            />
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">分类别名</label>
          <div class="layui-input-block">
            <input
              type="text"
              name="alias"
              required
              lay-verify="required"
              placeholder="请输入分类别名"
              autocomplete="off"
              class="layui-input"
            />
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">
              修改
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
          </div>
        </div>
      </form>
        `,
    });

    //需求5:展示当前的文章分类信息
    let id = $(this).attr("data-id");

    axios({
      method: "GET",
      url: "/my/article/cates/" + id,
      //此处的 :id 为动态参数,可以改变
      // : 必须省略
    }).then((res) => {
      console.log(res.data);
      if (res.data.status !== 0) {
        return layer.msg(res.data.message);
      }
      //展示
      form.val("formEdit", res.data.data);
    });
  });

  //需求5:修改文章分类 - 事件委托实现
  $("body").on("submit", "#formEdit", function (e) {
    e.preventDefault();
    axios({
      method: "POST",
      url: "/my/article/updatecate",
      data: $(this).serialize(),
    }).then((res) => {
      //console.log(res.data);

      //TODO:判断,关闭对话框,清空表单,刷新页面
      if (res.data.status !== 0) {
        return layer.msg(res.data.message);
      }
      //成功提示
      layer.msg("修改成功");
      layer.close(indexEdit);
      initArtCateList();
    });
  });

  //需求6: 删除功能 - 事件委托
  $("tbody").on("click", ".btn-deleted", function () {
    let id = $(this).attr("data-id");
    //eg1
    layer.confirm("来了,老弟?", { icon: 3, title: "提示" }, function (index) {
      //发送axios
      axios({
        method: "GET",
        url: "/my/article/deletecate/" + id,
      }).then((res) => {
        if (res.data.status !== 0) {
          return layer.msg(res.data.message);
        }
        //成功提示
        layer.msg(res.data.message);
        initArtCateList();
      });
      layer.close(index);
    });
  });
});
